package com.dragon.controller;

import com.dragon.pojo.Problem.Problem;
import com.dragon.pojo.Problem.ProblemSelectInfo;
import com.dragon.pojo.Problem.SubmitInfo;
import com.dragon.pojo.Problem.SubmitResult;
import com.dragon.pojo.Result;
import com.dragon.service.ProblemService;
import com.dragon.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("problem")
public class ProblemController {
    @Autowired
    private ProblemService problemService;

    /**
     * 查询题目列表
     * @param request 获取 token
     * @param info ProblemSelectInfo
     * @return 响应信息
     */
    @GetMapping("list")
    public Result getProblemList(HttpServletRequest request, ProblemSelectInfo info) {
        String jwt = request.getHeader("Accept-token");
        if(jwt !=null && !jwt.isEmpty()) {
            Claims claims = JwtUtils.parseJWT(jwt);
            if(claims != null){
                info.setUserId((Integer) claims.get("id"));
            }
        }
        return Result.success(problemService.getProblemList(info));
    }

    /**
     * 查询题目信息
     * @param request 获取 token
     * @param problemId 题目 id
     * @return 响应信息
     */
    @GetMapping("info/{problemId}")
    public Result getProblemInfo(HttpServletRequest request,@PathVariable Integer problemId) {
        String jwt = request.getHeader("Accept-token");
        Integer userId = null;
        if(jwt !=null && !jwt.isEmpty()) {
            Claims claims = JwtUtils.parseJWT(jwt);
            if(claims != null){
                userId = (Integer) claims.get("id");
            }
        }
        return Result.success(problemService.getProblemInfo(problemId, userId));
    }

    /**
     * 创建题目
     * @param request 获取token
     * @param problem Problem
     * @return 响应信息
     */
    @PostMapping("add")
    public Result addProblem(HttpServletRequest request, @RequestBody Problem problem) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行新增:\"{}\" 题目操作", claims.get("id"), problem.getTitleName());
        if(claims.get("permissions").equals("admin")){
            problem.setCreator((String) claims.get("nickname"));
            problemService.addProblem(problem);
            return Result.success("创建成功");

        }
        return Result.error("没有权限",403);
    }

    /**
     * 删除题目
     * @param request 获取token
     * @param problemId 问题id
     * @return 响应信息
     */
    @DeleteMapping("del")
    public Result deleteProblem(HttpServletRequest request, Integer problemId) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行删除:{} 题目操作", claims.get("id"), problemId);
        if(claims.get("permissions").equals("admin")){
            problemService.deleteProblem(problemId);
            return Result.success("删除成功");

        }
        return Result.error("没有权限",403);
    }

    /**
     * 提交结果
     * @param request 获取 token
     * @param info SubmitInfo
     * @return 响应结果
     */
    @PostMapping("submit")
    public Result submit(HttpServletRequest request, @RequestBody SubmitInfo info) {
        Claims claims = (Claims) request.getAttribute("claims");
        info.setUserId((Integer) claims.get("id"));
        log.info("用户:{} 进行提交运行题目:{} 操作", info.getUserId(), info.getProblemId());
        SubmitResult submit = problemService.submit(info);
        if(submit == null){
            return Result.error("题目不存在", 410);
        }
        return Result.success("提交成功", submit);
    }

    @PostMapping("test")
    public Result testSubmit(HttpServletRequest request, @RequestBody SubmitInfo info) {
        Claims claims = (Claims) request.getAttribute("claims");
        info.setUserId((Integer) claims.get("id"));
        log.info("用户:{} 进行测试运行题目:{} 操作", info.getUserId(), info.getProblemId());
        String output = problemService.testSubmit(info);
        return Result.success("提交成功", output);
    }

    /**
     * 获取全部题目信息
     * @param request 获取 token
     * @param problemId 问题的 id
     * @return 返回响应
     */
    @GetMapping("allInfo")
    public Result getAllProblemInfo(HttpServletRequest request, Integer problemId) {
        Claims claims = (Claims) request.getAttribute("claims");
        if(claims.get("permissions").equals("admin")){
            return Result.success(problemService.getAllProblemInfo(problemId));
        }
        return Result.error("没有权限",403);
    }

    /**
     * 修改题目
     * @param request 获取 token
     * @param problem Problem
     * @return 返回响应
     */
    @PutMapping("update")
    public Result UpdateProblem(HttpServletRequest request, @RequestBody Problem problem) {
        Claims claims = (Claims) request.getAttribute("claims");
        log.info("用户:{} 进行修改:{} 题目操作", claims.get("id"), problem.getId());
        if(claims.get("permissions").equals("admin")){
            problemService.updateProblem(problem);
            return Result.success("修改成功");

        }
        return Result.error("没有权限",403);
    }


}
